<template>
  <div class="Maintenance">
    <!-- header -->
    <div class="header">
      <h2>车辆年检登记</h2>
      <div class="buttons">
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="add"
          >新增</el-button
        >
        <el-button type="primary" icon="el-icon-share"></el-button>
        <el-button type="primary" icon="el-icon-delete"></el-button>
        <el-button type="primary" icon="el-icon-search">搜索</el-button>
        <el-button type="primary"
          >上传<i class="el-icon-upload el-icon--right"></i
        ></el-button>
      </div>
    </div>
    <!-- header -->
    <!--  -->
    <!-- table -->
    <el-table
      :data="
        tableData.filter(
          (data) =>
            !search ||
            data.carNumber.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%"
      height="650"
      border
    >
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column prop="carNumber" label="车辆牌号" width="240">
      </el-table-column>
      <el-table-column prop="annDate" label="年检时间" width="240" sortable>
      </el-table-column>
      <el-table-column prop="annFee" label="年检费用" width="240" sortable>
      </el-table-column>
      <el-table-column prop="dueDate" label="到期日期" width="240" sortable>
      </el-table-column>
      <el-table-column fixed="right" min-width="200">
        <template slot="header" slot-scope="">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">
            查看
          </el-button>
          <el-button type="text" size="small" @click="change(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            style="color: red"
            @click="del(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- table -->
    <!--  -->
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @current-change="current_change"
        background
        layout="prev, pager, next"
        :total="blockall"
      >
      </el-pagination>
    </div>
    <!-- 分页 -->
    <!--  -->
    <!-- 详情 -->
    <el-drawer
      title="年检记录"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="cancel"
      :size="500"
    >
      <div class="details">
        <div class="details-main">
          <el-form label-position="top" label-width="80px">
            <el-form-item label="车辆牌号">
              {{ details.carNumber }}
            </el-form-item>
            <el-form-item label="年检日期">
              {{ details.annDate }}
            </el-form-item>
            <el-form-item label="年检费用">
              {{ details.annFee }}
            </el-form-item>
            <el-form-item label="到期日期">
              {{ details.dueDate }}
            </el-form-item>
            <el-form-item label="经手人"> 张思瑾 </el-form-item>
          </el-form>
          <!-- 表单 -->
        </div>
        <div class="drawer__footer">
          <el-button type="primary" @click="cancel"> 确 定 </el-button>
        </div>
      </div>
    </el-drawer>
    <!-- 详情 -->
    <!--  -->
    <!-- 添加 -->
    <el-dialog title="新增" :visible.sync="dialogAddVisible">
      <div class="add">
        <el-form ref="addform" :rules="rules" :model="addform" label-width="80px">
          <el-form-item label="车辆牌号" prop="carNumber">
            <el-select v-model="addform.carNumber" placeholder="请选择">
              <el-option v-for="(v,index) in carlist" :key="index" :label="v.car_number" :value="v.car_number"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="年检时间" required>
            <el-col :span="11">
              <el-form-item prop="annDate">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="addform.annDate"
                style="width: 100%"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="年检费用" prop="annFee">
            <el-input v-model="addform.annFee" placeholder="请输入金额"></el-input>
          </el-form-item>
          <el-form-item label="到期时间" required>
            <el-col :span="11">
              <el-form-item prop="dueDate">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="addform.dueDate"
                style="width: 100%"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onAddSubmit(addform)"
              >提交</el-button
            >
            <el-button @click="dialogAddVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
    <!-- 添加 -->
    <!--  -->
    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="dialogChangeVisible">
      <div class="change">
        <el-form ref="details" :model="details" label-width="80px">
          <el-form-item label="车辆牌号">
            <el-select v-model="details.carNumber" placeholder="请选择">
              <el-option v-for="(v,index) in carlist" :key="index" :label="v.car_number" :value="v.car_number"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="车辆牌号">
            <el-input v-model="details.carNumber" placeholder="请输入车牌号"></el-input>
          </el-form-item> -->
          <el-form-item label="年检时间">
            <el-col :span="11">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="details.annDate"
                style="width: 100%"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="年检费用">
            <el-input
              v-model="details.annFee"
              placeholder="请输入金额"
            ></el-input>
          </el-form-item>
          <el-form-item label="到期时间">
            <el-col :span="11">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="details.dueDate"
                style="width: 100%"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onChangeSubmit(details)"
              >提交</el-button
            >
            <el-button @click="dialogChangeVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
    <!-- 编辑 -->
  </div>
</template>

<script>
import {
  // nxList,
  NxAllList,
  changenx,
  addnx,
  delnx,
  kannx,
  // javawblist,
} from "../../apis/zsj";
import {zVehicleInfo} from "../../apis/zyx/link"

export default {
  components: {},
  mounted() {
    //获取json
    //总页数
    NxAllList().then((ok) => {
      console.log(ok);
      this.tableData = ok.data.data.records;
      this.blockall = ok.data.data.total;
    });
    //分页查询
    // nxList().then((ok) => {
    //   console.log(ok)
    //   this.tableData = ok.data.data;

    // });
    zVehicleInfo().then((ok)=>{
      console.log(ok);
      this.carlist=ok.data.data
    })
  },
  methods: {
    //页码
    current_change(i) {
      console.log(i);
      this.index = i;
      // 分页查询
      // nxList(i).then((ok) => {
      //   console.log(ok);
      //   this.tableData = ok.data.date;
      // });
      NxAllList(i).then((ok) => {
      console.log(ok);
      this.tableData = ok.data.data.records;
    });
    },
    //编辑
    change(row) {
      this.details = row;
      this.dialogChangeVisible = true;
    },
    //编辑提交
    onChangeSubmit(obj) {
      console.log("新增", obj);
      if (this.dialogChangeVisible) {
        changenx(obj).then((ok) => {
          console.log(ok);
          if (ok.status === 200) {
            this.dialogChangeVisible = false;
          }
        });
      }
    },
    //新增按钮
    add() {
      this.dialogAddVisible = true;
    },
    //随机数
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    //随机id
    randomID() {
      let reID = "";
      for (let i = 0; i < 6; i++) {
        reID += this.randomNum(0, 9);
      }
      return reID / 1;
    },
    //新增提交
    onAddSubmit(obj) {
      console.log(obj);
      this.$refs["addform"].validate((valid)=>{
        if(valid){
          addnx(obj).then((ok) => {
            console.log(ok);
            if (ok.data.code === 200) {
              this.dialogAddVisible = false;
              NxAllList(this.index).then((ok) => {
                console.log(ok);
                this.tableData = ok.data.data.records;
                this.blockall = ok.data.data.total;
              });
            }
          });
        }
      })
      // obj.wb_id = this.randomID();
     
    }
    ,
    //删除
    del(row) {
      console.log("row"+row);
      delnx(row.id).then((ok) => {
        //总页数
        console.log(ok);
        // NxAllList().then((ok) => {
        //   this.blockall = ok.data.date.length;
        // });
        // //当前页
        NxAllList(this.index).then((ok) => {
          // console.log(ok.data.data.length);
          this.tableData = ok.data.data.records;
              this.blockall = ok.data.data.total;
          if (ok.data.data.records == 0) {
            console.log(this.index--);
            NxAllList(this.index).then((ok) => {
              this.tableData = ok.data.data.records;
              this.blockall = ok.data.data.total;
            });
          }
        });
      });
    },
    //详情取消
    cancel() {
      this.drawer = false;
    },
    // 关闭详情抽屉
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => {});
    },
    //查看
    handleClick(row) {
      console.log(row);
      this.drawer = !this.drawer;
      this.details = row;
      kannx(row.carNumber).then((ok) => {
        console.log(ok);
      });
    },
  },
  data() {
    return {
      //当前页码
      carlist:[],
      index: 1,
      //分页总数
      blockall: 1,
      // 编辑对话框
      dialogChangeVisible: false,
      //添加对话框
      dialogAddVisible: false,
      //抽屉
      direction: "rtl",
      drawer: false,
      //搜索
      search: "",
      //列表
      tableData: [],
      //详情
      details: {
        // carNumber: "",
        // annDate: "",
        // annFee: "",
        // dueDate: "",
      },
      //添加表单
      addform: {
        carNumber: "",
        annDate: "",
        annFee: "",
        dueDate: "",
      },
      rules: {
          carNumber: [
            { required: true, message: '请选择', trigger: 'change' },
          ],
          annDate: [
            { type: 'string', required: true, message: '请选择日期', trigger: 'change' }
          ],
          dueDate: [
            { type: 'string', required: true, message: '请选择日期', trigger: 'change' }
          ],
          annFee: [
            {  required: true, message: '请填写金额', trigger: 'blur' }
          ],

        }
     
    };
  },
};
</script>

<style lang="scss" scoped>
.Maintenance {
  // height: 100%;
  // width: auto;
  // min-width: 444px;
  min-height: 800px;
  background-color: white;
  padding: 10px;
  box-sizing: border-box;
  border-radius: 5px;
  position: relative;

  //头部
  .header {
    h2 {
      margin: 5px 0;
      font-size: 18px;
      font-weight: 400;
    }
    .buttons {
      margin: 20px 0;
    }
  }
  //详情
  .details {
    height: 100%;
    display: flex;
    flex-direction: column;
    .details-main {
      margin-top: 50px;
      padding: 20px;
      flex: 1;
    }
    .drawer__footer {
      display: flex;
      justify-content: center;
    }
  }
  //分页
  .block {
    margin: 10px 0;
  }
}
</style>